package com.gzdh.dishrecognition.ui.dialog;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.alibaba.fastjson.JSONArray;
import com.dhkj.web.utils.MyObserverRes;
import com.dhkj.web.utils.RequestUtils;
import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;
import com.gzdh.dishrecognition.R;
import com.gzdh.dishrecognition.ui.adapter.report.DiningTableDetailsAdapter;

import java.util.concurrent.TimeUnit;

public class DiningTableDetailDialog extends Dialog {

    private final Cache<String,String> cache = CacheBuilder.newBuilder().maximumSize(1000).
            expireAfterWrite(3, TimeUnit.SECONDS).build();

    private Context mContext;

    private Integer type;

    private JSONArray jsonArray;

    private String ksh;

    private String dinner;

    private String dcTime;
    private DiningTableDetailsAdapter diningTableDetailsAdapter;

    private FinalDispose dispose;

    public void setFinalDispose(FinalDispose dispose) {
        this.dispose = dispose;
    }

    public interface FinalDispose {
        void reload();
    }

    @SuppressLint("SetTextI18n")
    public void setJsonArray(JSONArray jsonArray) {
        this.jsonArray = jsonArray;
        diningTableDetailsAdapter.setJsonArray(jsonArray);
    }

    public DiningTableDetailDialog(@NonNull Context context, Integer type, String ksh, String dinner, String dcTime) {
        super(context, R.style.CustomDialog);
        this.type = type;
        this.ksh = ksh;
        this.dinner = dinner;
        this.dcTime = dcTime;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_dining_table_details);
        mContext = this.getContext();
        RecyclerView ordersRv = findViewById(R.id.ordersRv);
        diningTableDetailsAdapter = new DiningTableDetailsAdapter(mContext);
        LinearLayoutManager layoutManager = new LinearLayoutManager(mContext);
        ordersRv.setLayoutManager(layoutManager);
        ordersRv.setAdapter(diningTableDetailsAdapter);
        View cleanTv = findViewById(R.id.cleanTable);
        if(type == 0) {
            cleanTv.setVisibility(View.VISIBLE);
        } else {
            cleanTv.setVisibility(View.INVISIBLE);
        }
        cleanTv.setOnClickListener(view -> {
            if(cache.getIfPresent("clean") != null) {
                return;
            }
            cache.put("clean", String.valueOf(System.currentTimeMillis()));
            RequestUtils.cleanDiningTable(mContext, ksh, dinner, dcTime, new MyObserverRes<>(mContext) {
                @Override
                public void onSuccess(String result) {
                    Toast.makeText(mContext,"翻台成功！", Toast.LENGTH_SHORT).show();
                    if(dispose != null) {
                        dispose.reload();
                        dismiss();
                    }
                }

                @Override
                public void onFailure(Throwable e, String errorMsg) {
                    Toast.makeText(mContext, errorMsg, Toast.LENGTH_LONG).show();
                }
            });
        });
    }
}
